//
//  APOApalloAPI.m
//  Apallo
//
//  Created by Jason Poh on 27/8/14.
//  Copyright (c) 2014 Apallo. All rights reserved.
//

#import "APOApalloAPI.h"
#import "APOConstants.h"
#import "APOUser.h"

@implementation APOApalloAPI

NSString *const APALLO_API_NOTIFICATION = @"APALLO_API_NOTIFICATION";

+(void)getInfo: (NSString*) ivleToken
{
    NSLog(@"APOApalloAPI#getInfo: Getting user info");
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString * url_str = @"http://swiftcreate.com/apallo/getInfo.php";
    NSDictionary *params = @{@"token": ivleToken };
    
    [manager POST:url_str parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *response_str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"APOApalloAPI#getInfo: userID = %@", response_str);
        
        
        
        NSString * db_id = @"";
        NSString * userid = @"";
        NSString * photo = @"";
        NSString * faculty = @"";
        NSString * nickname = @"";
        NSString * description = @"";
        NSString * interests = @"";
        NSString * major = @"";

        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *responseDict = responseObject;
            NSLog(@"APOApalloAPI#getInfo: is NSDicttionary");
            db_id = [[NSString alloc] initWithFormat:@"%@",[responseDict valueForKey:@"id"] ];
            userid = [[NSString alloc] initWithFormat:@"%@",[responseDict valueForKey:@"userid"] ];
            photo = [[NSString alloc] initWithFormat:@"%@",[responseDict valueForKey:@"photo"] ];
            faculty = [[NSString alloc] initWithFormat:@"%@",[responseDict valueForKey:@"faculty"] ];
            nickname = [[NSString alloc] initWithFormat:@"%@",[responseDict valueForKey:@"nickname"] ];
            description = [[NSString alloc] initWithFormat:@"%@",[responseDict valueForKey:@"description"] ];
            interests = [[NSString alloc] initWithFormat:@"%@",[responseDict valueForKey:@"interests"] ];
            
        }
        if ([responseObject isKindOfClass:[NSArray class]]) {
            NSLog(@"APOApalloAPI#getInfo: is NSArray");
        }
        
        APOUser* user = [APOUser getUser];
        user.student_details.db_id = db_id;
        user.student_details.matric_no = userid;
        user.student_details.photo = photo;
        user.student_details.photo_small = photo;
        user.student_details.faculty = faculty;
        user.student_details.nickname = nickname;
        user.student_details.description = description;
        user.student_details.interests = interests;
        user.student_details.major = major;
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"APOApalloAPI#getInfo: Error %@", error);
    }];

}

+ (void)postNotificationWithString:(NSString *)data :(NSString*) key //post notification method and logic
{
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:data forKey:key];
    [[NSNotificationCenter defaultCenter] postNotificationName:APALLO_API_NOTIFICATION object:nil userInfo:dictionary];
}

@end
